package com.example.weather.city_manager

import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemClickListener
import android.widget.ArrayAdapter
import android.widget.EditText
import android.widget.GridView
import android.widget.ImageView
import android.widget.Toast
import com.example.weather.BaseActicity
import com.example.weather.MainActivity
import com.example.weather.R
import com.example.weather.WeatherBean
import com.example.weather.data.WeatherDatabase
import com.google.gson.Gson


/**
 * 搜索城市活动类，负责处理城市搜索和数据显示
 * @author Y7993
 */
class SearchCityActivity : BaseActicity(), View.OnClickListener {
    var searchEt: EditText? = null
    var submitIv: ImageView? = null
    var searchGv: GridView? = null
    var hotCitys: Array<String> = arrayOf<String>(
        "北京",
        "上海",
        "广州",
        "深圳",
        "珠海",
        "佛山",
        "南京",
        "苏州",
        "厦门",
        "长沙",
        "成都",
        "福州",
        "杭州",
        "武汉",
        "烟台",
        "西安",
        "太原",
        "沈阳",
        "重庆",
        "天津",
        "南宁"
    )
    var url1: String =
        "https://wis.qq.com/weather/common?source=pc&weather_type=observe|index|rise|alarm|air|tips|forecast_24h&province="
    var url2: String = "&city="
    var city: String? = null
    var provice: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_search_city)
        searchEt = findViewById<EditText>(R.id.search_et)
        submitIv = findViewById<ImageView>(R.id.search_iv_submit)
        searchGv = findViewById<GridView>(R.id.search_gv)
        submitIv!!.setOnClickListener(this)
        //        设置适配器
        val adapter = ArrayAdapter<String?>(this, R.layout.item_hotcity, hotCitys)
        searchGv!!.adapter = adapter
        setListener()
        //数据库
        val weatherDatabase = WeatherDatabase.getDatabase(this)
        weatherDatabase.getCityDao()
    }

    /**
     * 设置网格视图的点击事件
     */
    private fun setListener() {
        searchGv!!.onItemClickListener =
            OnItemClickListener { adapterView: AdapterView<*>?, view: View?, pos: Int, id: Long ->
                city = hotCitys[pos]
                provice = GetProvice(city!!)
                val url = url1 + provice + url2 + city
                searchEt!!.setText(city)
                loadData(url)
            }
    }

    /**
     * 根据城市名称获取省份
     *
     * @param city 城市名称
     * @return 省份名称
     */
    private fun GetProvice(city: String): String {
        val Citys = arrayOf<String?>(
            "北京",
            "上海",
            "广东省 广州",
            "广东省 深圳",
            "广东省 珠海",
            "广东省 佛山",
            "江苏省 南京",
            "江苏省 苏州",
            "福建省 厦门",
            "湖南省 长沙",
            "四川省 成都",
            "福建省 福州",
            "浙江省 杭州",
            "湖北省 武汉",
            "山东省 烟台",
            "陕西省 西安",
            "山西省 太原",
            "辽宁省 沈阳",
            "重庆",
            "天津",
            "广西省 南宁"
        )
        for (s in Citys) {
            if (s!!.contains(city)) {
                provice = s.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()[0]
                break
            }
        }
        return provice!!
    }

    override fun onClick(view: View) {
        if (view.id == R.id.search_iv_submit) {
            city = searchEt!!.text.toString()
            if (!TextUtils.isEmpty(city)) {
                provice = GetProvice(city!!)
                val url = url1 + provice + url2 + city
                loadData(url)
            } else {
                Toast.makeText(this, "搜索城市不能为空", Toast.LENGTH_LONG).show()
            }
        }
    }

    override fun onSuccess(result: String?) {
        val weatherBean = Gson().fromJson<WeatherBean>(result, WeatherBean::class.java)
        if (weatherBean.data.index.clothes != null) {
            val intent = Intent(this, MainActivity::class.java)
            intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
            intent.putExtra("city", city)
            startActivity(intent)
        } else {
            Toast.makeText(this, "暂时未收入此城市天气信息...", Toast.LENGTH_SHORT).show()
        }
    }
}
